//
// Created by Administrator on 2022/1/2.
//

#ifndef CPP_LEARNING_BIN_NODE_TEST_HPP
#define CPP_LEARNING_BIN_NODE_TEST_HPP

#include "../src/bin_node.hpp"

#include <iostream>
using namespace std;

namespace BIN_NODE_TEST{

    template <typename T>
    struct Visit{
        void operator() ( T& e){
            cout<<e<<" ";
        }
    };



/**
 * 对这样的树进行测试
 *                                                  8
 *                        4                                                        11
 *
 *           2                        6                              9                         12
 *     1           3            5            7                                10
 */

    template <typename T>
    void build_node_relation(BinNodePosi(T)p, BinNodePosi(T)lc, BinNodePosi(T)rc){
        p->lc = lc;
        p->rc = rc;
        if(lc){
            lc->parent = p;
        }
        if(rc){
            rc->parent = p;
        }
    }
    void bin_node_test(){
        BinNodePosi(int) nodes[13];
        for(int i=1; i<=12; i++){
            nodes[i] = new BinNode<int>(i,0);
        }

        build_node_relation(nodes[8],nodes[4],nodes[11]);
        build_node_relation(nodes[4],nodes[2],nodes[6]);
        build_node_relation(nodes[2],nodes[1],nodes[3]);
        build_node_relation(nodes[6],nodes[5],nodes[7]);

        build_node_relation(nodes[11],nodes[9],nodes[12]);
        build_node_relation(nodes[9],(BinNodePosi(int))0,nodes[10]);

        cout<<"node[8] tree size: "<<nodes[8]->size()<<endl;

        Visit<int> visit;
        nodes[8]->travIn(visit);
        cout<<endl;
        nodes[8]->travPre(visit);
        cout<<endl;
        nodes[8]->travPost(visit);
        cout<<endl;
        nodes[8]->travLevel(visit);
        cout<<endl;


        cout<<"node[10] succ "<<nodes[10]->succ()->data<<endl;
        cout<<"node[1] succ "<<nodes[1]->succ()->data<<endl;
        cout<<"node[7] succ "<<nodes[7]->succ()->data<<endl;
    }
}

#endif //CPP_LEARNING_BIN_NODE_TEST_HPP
